<?php

/**
 * Description of backofficeCreateAction
 \* www.teuker.nl - mail@teuker.nl
 *
 * @author francisc
 */

class backofficeCreateAction extends sfAction {

	public function execute($request) {
		$this->form = new ReservationBackofficeForm();

		if ( $this->form->getValue('person_no') >= $this->getUser()->getAttribute('group_size')) {
			$resstatus = 2;
		} else {
			$resstatus = 1;
		}
		$params = $request->getParameter($this->form->getName());
		$params['date'] = Util::convertDateClientServer($params['date'], 'nl');
		$date=$params['date'];
		$time=$params['time_hour'].":".$params['time_minute'];
		$isValidTime=time()<=strtotime($date.' '.$time);

		if ($this->processBackofficeReservation($request, $this->form)&&$isValidTime) {
			$token = md5(time());
			SqlEngineCore::get()->insert(
			array('table' => 'reservation',
                          'columns' => array(
                                'restaurant_id' => array('value' => $this->getUser()->getAttribute('current_restaurant_id'), 'type' => PDO::PARAM_INT),
                                'user_id' => $this->getUser()->getAttribute('user_id'),
                                'menu_id' => $this->form->getValue('menu_id'),
                                'created_at' => 'NOW()',
                                'date' => $this->form->getValue('date'),
                                'occasion' => $this->form->getValue('occasion'),
                                'time' => $this->form->getValue('time_hour') . ':' . $this->form->getValue('time_minute'),
                                'person_no' => array('value' => $this->form->getValue('person_no'), 'type' => PDO::PARAM_INT),
                                'salutation' => array('value' => $this->form->getValue('salutation'), 'type' => PDO::PARAM_INT),
                                'name_last' => $this->form->getValue('name_last'),
                                'name_first' => $this->form->getValue('name_first'),
                                'name_company' => $this->form->getValue('name_company'),
                                'address' => $this->form->getValue('address'),
                                'phone' => $this->form->getValue('phone'),
                                'mobile' => $this->form->getValue('mobile'),
                                'email' => $this->form->getValue('email'),
                                'comments_internal' => $this->form->getValue('comments_internal'),
                                'comments_diet' => $this->form->getValue('comments_diet'),
                                'comments_general' => $this->form->getValue('comments_general'),
                    			'resstatus' => $resstatus,
                                'token' => $token)));

			$reservationId = SqlEngineCore::get()->getLastInsertId();
			$reservation = SqlEngine::getReservations(array('reservation_id' => $reservationId));
		
			Util::send_thank_you_email($reservation[0]);

			$this->redirect('@reservation_list?date=' . $this->form->getValue('date'));
		}
		else {
			$this->setTemplate('backoffice');
		}
	}

	protected function processBackofficeReservation(sfWebRequest $request, sfForm $form) {
		$params = $request->getParameter($form->getName());
		$params['date'] = Util::convertDateClientServer($params['date'], 'nl');

		$form->bind($params);

		$is_block_time = Util::is_block_time($params['date'],
		$params['time_hour'],
		$params['time_minute'],
		$this->getUser());

		$is_seat_limit = Util::is_seat_limit($params['person_no'],
		$params['date'],
		$params['time_hour'],
		$params['time_minute'],
		$this->getUser());

		if ($is_block_time['is_block_time']) {
			$request->setParameter('block_time', true);
			$request->setParameter('block_time_reason', $is_block_time['reason']);
		}
		else if ($is_seat_limit)
		$request->setParameter('seat_limit', true);

		return $form->isValid() && !$is_block_time['is_block_time'];
	}
}
